﻿Public Class clsAutoNumber
    Public Function getNextKeyNo(code As String, key As String) As Integer
        Dim szSql As String = ""
        szSql = "SELECT * FROM sf.t_auto_number WHERE no_key = '" & key & "' and code = '" & code & "'"
        Dim dtX As DataTable = DBacs.ExecuteSql(szSql, -1)
        If IsNothing(dtX) Then
            getNextKeyNo = 1
        Else
            getNextKeyNo = dtX.Rows(0)("no_value") + 1
        End If

    End Function

    Public Function getGYH_Header() As String
        Dim ascYear As Integer = Asc("F")
        Dim gyhH As String = ""
        If Now.Year - 2021 + ascYear > 90 Then
            gyhH = "A"
        Else
            gyhH = Chr(Now.Year - 2021 + ascYear)
        End If
        Return "TS" & gyhH & "-"
    End Function

    Public Sub setNextKeyNo(code As String, key As String)
        Dim szSql As String
        szSql = String.Empty
        If CInt(getNextKeyNo(code, key)) = 0 Then
            szSql &= "INSERT INTO sf.t_auto_number(no_key, no_value, record_date, code) VALUES (?, ?,?,?)"
            DBacs.ExecuteUpdateSql(szSql, -1, key, getNextKeyNo(code, key), Now.ToString("yyyyMMdd"), code)
        Else
            szSql &= "update sf.t_auto_number set no_value = ? , record_date =? where no_key = ?  and code = ?"
            DBacs.ExecuteUpdateSql(szSql, -1, getNextKeyNo(code, key), Now.ToString("yyyyMMdd"), key, code)
        End If
    End Sub

    Public Function getNextProjectNo(_code As String, _name As String) As Integer
        Dim szSql As String = "SELECT no_value FROM sf.b_pgd_mes_project_auto_number WHERE mes_project_code = ? and mes_project_name = ?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, _code, _name)
        If dt.Rows.Count = 0 Then
            getNextProjectNo = 1
        Else
            getNextProjectNo = dt.Rows(0)("no_value") + 1
        End If
    End Function
    Public Sub setNextProjectValue(_code As String, _name As String)
        Dim szSql As String = "update sf.b_pgd_mes_project_auto_number set no_value = ?  where mes_project_code = ? and mes_project_name = ?"
        DBacs.ExecuteUpdateSql(szSql, -1, getNextProjectNo(_code, _name), _code, _name)
    End Sub
End Class
